<template>
  <div>
    <basic-container>
      <el-breadcrumb separator="/">
        <el-breadcrumb-item>数据</el-breadcrumb-item>
        <el-breadcrumb-item>书籍</el-breadcrumb-item>
        <el-breadcrumb-item>新增阅读统计</el-breadcrumb-item>
      </el-breadcrumb>
    </basic-container>
    <basic-container>
      <avue-crud
        :table-loading="loading"
        ref="crud"
        :data="data"
        v-model="form"
        :option="option"
        :page.sync="page"
        @on-load="getList"
        @refresh-change="refreshChange"
        @search-reset="from = {}"
        @search-change="searchChange"
      >
        <!-- 自定义操作栏 -->
        <!-- <template slot-scope="scope" slot="menu">
          <el-button
            icon="el-icon-primary"
            type="text"
            size="small"
            @click.stop="info(scope)"
            >查看章节详情</el-button
          >
        </template> -->
        <!-- 自定义按钮 -->
        <template slot-scope="scope" slot="menuLeft">
          <el-button
            type="primary"
            icon="el-icon-download"
            size="small"
            plain
            @click="handleExcel()"
            >导出Excel</el-button
          >
        </template>
      </avue-crud>
    </basic-container>
  </div>
</template>

<script>
import { handleExcelCat } from "@/api/exportCommon";
import { common } from "@/api/cat/httpRequest";
import { catUrl } from "@/config/env";
import { appStatisticsBook as baseURL,book,bookCategory } from "@/api/cat/baseUrls";
export default {
  async created() {
    let data = await  common(`${bookCategory.getListByLeave}/1`)
    if(data.code==1){
      this.option.column[4].dicData = data.data;
    }
  },
  data() {
    return {
      loading:false,
      page: {
        total: 0,
        pageSize: 10
      },
      type: true,
      data: [],
      params: {},
      form: {},
      default: {
        orderByList: "statistics_date&DESC,chapter_num&DESC",
      },
      option: {
        index: true,
        indexLabel: "序号",
        addBtn: false,
        editBtn: false,
        viewBtn: false,
        delBtn: false,
        excelBtn: false,
        selection: true,
        border: true,
        menu: false,
        column: [
          {
            label: "日期",
            prop: "statistics_date_search",
            type: "daterange",
            search: true,
            hide:true
          },
          {
            label: "日期",
            prop: "statistics_date",
            type: "date",
            format:"yyyy-MM-dd",
            valueFormat: "yyyy-MM-dd",
            search: false,
          },
          {
            label: "图书ID",
            prop: "book_id",
            type: "input",
            search:true  
          },
          {
            label: "图书名称",
            prop: "book_title",
            type: "input",
            search:true
          }, 
          {
            label: "分类",
            prop: "category_id",
            type: "select",
            dicData: [],
            props: {
              label: "label",
              value: "value",
            }
          },
          {
            label: "阅读章节数",
            prop: "chapter_num",
            type: "input",
            width: '110px'
          },
          {
            label: "阅读人数",
            prop: "read_uv",
            type: "input",
          },
          {
            label: "人均阅读章节数",
            prop: "avg_chapter_num",
            type: "input",
            width: '110px'
          },
          {
            label: "排序",
            prop: "order",
            type: "select",
            hide: true,
            search: true,
            dicData: [
              {
                label: "累计阅读章节数升序",
                value: ',chapter_num&ASC',
              },
              {
                label: "累计阅读章节数降序",
                value: ',chapter_num&DESC',
              },
              {
                label: "累计阅读人数升序",
                value: ',read_uv&ASC',
              },
              {
                label: "累计阅读人数降序",
                value: ',read_uv&DESC',
              },
              {
                label: "人均阅读人数升序",
                value: ',avg_chapter_num&ASC',
              },
              {
                label: "人均阅读人数降序",
                value: ',avg_chapter_num&DESC',
              },
            ],
          }
        ]
      }
    };
  },
  methods: {
    //数据导出
     async handleExcel() {
       this.loading = true;
       //处理搜索条件
       let params = {...this.$refs.crud.searchForm};
        this.default.orderByList = "statistics_date&DESC";
        this.default.orderByList = this.default.orderByList+params.order;
        delete params.order;
        params.book_title = `${params.book_title ? '%'+params.book_title+'%' : ""}`;
       if(params.statistics_date_search.length>0){
          params.statistics_date = `${params.statistics_date_search.length>0 ? "!<>!" + params.statistics_date_search[0]+","+ params.statistics_date_search[1] : ""}`
       }
       delete params.statistics_date_search;

       //调用导出接口
       handleExcelCat(
         this.$export,
         baseURL.getBookNewUserList,
         { ...params, ...this.default },
         "书籍新增阅读统计",
         this.page.total,
         [...this.option.column],
         {
           handleIndexArr:[4],
           selectNameArr:["category_id"],
           sourceNameArr:["value"],
           sourceValueArr:["label"]
         }
       );
      this.loading = false;
    },
    /**获取列表 */
    async getList() {
      this.loading = true;
      //清空表格列表
      this.data = [];
      //发送请求获取数据
      let response = await common(
        baseURL.getBookNewUserList,
        { ...this.params, ...this.default },
        this.page,
        "POST"
      );
      this.page = response.page;
      this.data = response.data;
      this.loading = false;
    } /**刷新 */,
    refreshChange() {
      this.getList();
    },
    /**搜索 */
    searchChange(params, done) {
      this.default.orderByList = "statistics_date&DESC";
      this.default.orderByList = this.default.orderByList+params.order;
      delete params.order;
      params.book_title = `${params.book_title ? '%'+params.book_title+'%' : ""}`;
      params.statistics_date = `${params.statistics_date_search ? "!<>!" + params.statistics_date_search : ""}`
      delete params.statistics_date_search;
      this.params = params;
      this.page.currentPage = 1;
      this.getList();
      if (done) done();
    }
  },
};
</script>